<template>
    <div id="roleAdd" class="mt10">
        <div>
            <el-form ref="addForm" :model="addForm" :rules="rules" class="formEmbedTable mt20">
                <table>
					<caption>Add</caption>
                    <tr>
                        <td>Role Name</td>
                        <td>
                            <el-form-item prop="roleName">
                                <el-input v-model="addForm.roleName"></el-input>
                            </el-form-item>
                        </td>
                        <td> Description</td>
                        <td >
                            <el-form-item prop="roleDesc">
                                <el-input v-model="addForm.roleDesc" autosize></el-input>
                            </el-form-item>
                        </td>
                        				
                    </tr>
                    <tr>
                        <td>Role Category</td>
                        <td colspan='3'>{{"Branch"}}</td>				
                    </tr>
                </table>
            </el-form>  
        </div>
        <div class="mt20">
			<ynet-tree  @modifyMenuTree="modifyMenuTree" :menuTree="menuTree"></ynet-tree>
		</div>
        <div class="btnArea mt20 tac">
            <el-button type="primary" @click="queryAuth('addForm')">Confirm</el-button>
            <!--<el-button type="primary">重置</el-button>-->
            <el-button type="primary" @click="backMenu">Cancel</el-button>
        </div>
        <!--授权确认页面弹窗-->
        <div>
            <ynet-authDialog ref="dialogTable"  @getAuthForm="getAuthForm"></ynet-authDialog>
        </div>
    </div>
</template>
<script>
    import ghbIcon from '@src/components/icon'
    export default {
        name:'roleAdd',
        components:{
            ghbIcon,
        },
        props:[],
        mounted(){
            this.menuTree=this.$tools.storage.get("menuCopy", "save")//从缓存中获取操作员菜单树
        },
        data(){
            return{
                roleBsnList:[],dialogTableVisible:false,
                menuTree:[],//从缓存中获取操作员的菜单信息树
                addForm:{
                    roleName:'',roleType:'2',roleDesc:'',imBsnList:[],
                    actionFlag:'0',bsnCode:'IM0102',actionType:'0',
                },
                roleType:'Branch',
                imBsnList:[],
                lll:[],
                authForm: { authUserNo: '', password: '',randomField:''},//100004   617100
                rules:{
                    roleName:[
                        {required:true,message: 'required',},
                        {validator:this.$tools.validator.isCnEngNum,trigger:'blur',}
                    ],
                    roleType:[{required:true,message: 'required',}],
                },
                needAuth:'',
                authType:'', 
                temp:{bsnCode:'',bsnName:'',menuFlag:'',menuAuth:''},//临时数据
                imBsnListTemp:[],
                imBsnListTemp1:[],
                imBsnListTemp2:[],
            }
        },
        methods:{
            getAuthForm(val){
                //获取授权数据
                this.authForm=val;
                console.log("val:"+JSON.stringify(val))
                this.addForm=$.extend(this.addForm,this.authForm)
                this.submit()
            },
            backMenu(){
                this.$emit('backMenu');
            },
            modifyMenuTree(val){
                this.imBsnList=val ;//从树组件传给后台的菜单权限
                console.log("val:"+JSON.stringify(val))
            },
            queryAuth(formName){//查询授权模式
                this.$refs[formName].validate((valid)=>{
                    if(valid){
                        
                        if(this.imBsnList.length!=0){
                            let body={};body.bsnCode="IM0102";body.version="IM0102",
                            this.$tools.request(this,"IM010001Action.do",body).then(data=>{
                                if(data.body.errorCode==0){
                                    if(data.body.needAuth==0){//无需授权
                                        this.submit()
                                    }else if(data.body.needAuth==1 && data.body.authType==1){
                                        this.needAuth=1;
                                        this.authType=1;
                                        //this.dialogTableVisible=true;
                                        this.$refs["dialogTable"].show()
                                    }else if( data.body.needAuth==1 && data.body.authType==2 ){
                                        this.needAuth=1;
                                        this.authType=2;
                                        this.submit()
                                    }else{
                                        console.log("data:"+JSON.stringify(data))
                                        this.$alert('Authorization type error', 'warning', {
                                            confirmButtonText: 'Confirm',
                                            callback: action => {
                                            }
                                        });
                                    }
                                }else{
                                    this.$alert("System Error Try again", "warning",{
                                        confirmButtonText: 'Confirm',
                                        callback: action => {
                                        }
                                    })
                                }
                                
                            })
                        }else{
                            this.$alert("请选择菜单",{
                                confirmButtonText: 'Confirm',
                                callback: action => {
                                }
                            })
                        }
                        
                    }
                })               
			},
            submit(){
                //将父菜单封装到数据里 根据bsnCode
                this.imBsnListTemp=this.imBsnList;
                for(let i=0;i<this.imBsnList.length;i++){
                    if(this.imBsnList[i].bsnCode.length==6){
                        let temp={bsnCode:'',bsnName:'',menuFlag:'',menuAuth:''};
                        temp.bsnCode=this.imBsnList[i].bsnCode.substring(0,4);
                        //temp.menuFlag=this.imBsnList[i].menuFlag;
                        temp.menuAuth=this.imBsnList[i].menuAuth;
                        temp.menuFlag=1;
                        //temp.menuAuth=0;
                        //先去掉重复的
                        for(let j=0;j<this.imBsnListTemp.length;j++){
                            if(temp.bsnCode==this.imBsnListTemp[j].bsnCode){
                                this.imBsnListTemp.splice(j,1)
                            }
                        }
                        //从新放入数据
                        this.imBsnListTemp.push(temp)
                    }
                    if(this.imBsnList[i].bsnCode.length==8){
                        let temp1={bsnCode:'',bsnName:'',menuFlag:'',menuAuth:''};
                        temp1.bsnCode=this.imBsnList[i].bsnCode.substring(0,6);
                        //temp1.menuFlag=this.imBsnList[i].menuFlag;
                        temp1.menuAuth=this.imBsnList[i].menuAuth;
                        temp1.menuFlag=1;

                        for(let j=0;j<this.imBsnListTemp.length;j++){
                            if(temp1.bsnCode==this.imBsnListTemp[j].bsnCode){
                                this.imBsnListTemp.splice(j,1)
                            }
                        }
                        this.imBsnListTemp.push(temp1)
                    }
                }
                //console.log("imBsnListTemp_result:"+JSON.stringify(this.imBsnListTemp))
                this.addForm.imBsnList=this.imBsnListTemp
                this.$tools.request(this,"IM010202Action.do",this.addForm).then(data=>{
                    if(data.body.errorCode==0){
                        if(this.needAuth==1 && this.authType==2){
                            this.$message.success('Waiting for Authorized')
                            this.$emit('backMenu');
                        }else if(this.needAuth==1 && this.authType==1){
                            if(data.body.authStatus==1){
                                this.$message.error({
                                     message:'Authorship No. or password wrong ,please resume load',
                                    showClose:true,
                                })
                            }else{
                                this.$message.success('Add Success')
                                this.$emit('backMenu');
                            }
                        }else{
                            this.$message.success('Add Success')
                            this.$emit('backMenu');
                        }
                        
                    }else{
                        this.$alert("System Error Try again", "warning",{
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        })
                    }
                })
            },
        }
    }
</script>